package com.wisdudu.blank.ui.setting.system;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.wisdudu.blank.R;
import com.wisdudu.frame.base.BaseFragment;

/**
 * Created by ypp on 2016/8/17.
 */
public class WebViewFragment extends BaseFragment {

    public static String EXTRA_TITLE = "title";
    public static String EXTRA_URL = "url";
    //    private ActivityWebviewBinding mBinding;
    private WebView mWebView;
    private ProgressBar mProgressBar;

    @Override
    protected View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        mBinding = DataBindingUtil.inflate(inflater, R.layout.activity_webview, container, false);
        View view = inflater.inflate(R.layout.activity_webview, container, false);
        mWebView = (WebView) view.findViewById(R.id.webView);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolbar(getToolbar(), getArguments().getString(EXTRA_TITLE));
//        LoadingDialog loadingDialog = new LoadingDialog(getActivity(), "正在加载") {
//            @Override
//            public void cancel() {
//
//            }
//        };
//        loadingDialog.show();
        String url = getArguments().getString(EXTRA_URL);
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                loadingDialog.dismiss();
                mProgressBar.setVisibility(View.GONE);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
//                    mProgressBar.setVisibility(View.GONE);
                } else {
//                    if (View.INVISIBLE == mProgressBar.getVisibility()) {
//                        mProgressBar.setVisibility(View.VISIBLE);
//                    }
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
    }
}
